﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;
using LitJson;

namespace FileNameCheck
{

    public enum TextureType
    {
        OTHER = 0,
        COLOR_WITHOUT_ALPHA = 1,
        COLOR_WITH_ALPHA = 2,
        NORMAL = 3,
        TRANSPARENT_TEX = 4,
    }

#if CXImage
    public class TextureHelper
    {
#if !DEBUG

        [DllImport("cximagecrt.dll")]
        public extern static int GetDllVersion();

        [DllImport("cximagecrt.dll")]
        public extern static IntPtr LoadFile(string filename,int type);

        [DllImport("cximagecrt.dll")]
        public extern static int CheckType(IntPtr prt);

        [DllImport("cximagecrt.dll")]
        public extern static bool Destory(IntPtr prt);
#else
        [DllImport("cximagecrtd.dll")]
        public extern static int GetDllVersion();

        [DllImport("cximagecrtd.dll")]
        public extern static IntPtr LoadFile(string filename,int type);

        [DllImport("cximagecrtd.dll")]
        public extern static int CheckType(IntPtr prt);

        [DllImport("cximagecrtd.dll")]
        public extern static bool Destory(IntPtr prt);
#endif

    }
#endif


    public class TextureHelper
    {
        protected static TextureHelper _Instance = null;

        public static TextureHelper Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = CreateInstance();
                }
                return _Instance;

            }
        }

        protected static TextureHelper CreateInstance()
        {
            TextureHelper r = new TextureHelper();
            r.proc = new Process();
            r.proc.StartInfo.CreateNoWindow = true;
            r.proc.StartInfo.RedirectStandardOutput = true;
            r.proc.StartInfo.RedirectStandardInput = true;
            r.proc.StartInfo.UseShellExecute = false;
            r.proc.StartInfo.FileName = @"PillowCheck.exe";
            //r.proc.StartInfo.Arguments = @"-h";
            //r.proc.Start();
            return r;
        }

        protected string _Start(string arg = null)
        {
            if(!string.IsNullOrEmpty(arg))
            {
                proc.StartInfo.Arguments = arg;
            }    
            bool b = proc.Start();
            if (b)
            {
                return proc.StandardOutput.ReadToEnd();
            }
            else
            {
                return "Excute fail.";
            }
        }

        protected string _Start(string cmd, string arg)
        {
            //proc.StandardInput.AutoFlush = true;
            proc.StandardInput.WriteLine($"{cmd} {arg}");
            proc.StandardInput.Flush();
            while (!proc.StandardOutput.EndOfStream)
            {
                return proc.StandardOutput.ReadToEnd();
            }
            //string s = proc.StandardOutput.ReadToEnd();
            //Console.WriteLine(s);
            //return s;
            return @"{""size"":[100,100],""type"":3}";
        }

        protected Process proc = null;

#if DEBUG
        public static void Test()
        {
            string s = Instance._Start();
            Console.WriteLine(s);
        }
#endif

        public static string Start(string arg)
        {
            return Instance._Start(arg);
        }

        public static string Start(string cmd,string arg)
        {
            return Instance._Start(cmd,arg);
        }
    }


    public class TextureCheck
    {
        protected string _FileFullName;
        protected FileInfo _File;
        protected IntPtr _PtrXImg;
        protected TextureType _TextureType;

        public TextureType Type
        {
            get
            {
                return _TextureType;
            }
        }

        public TextureCheck(string filename)
        {
            _FileFullName = filename;

            _File = new FileInfo(filename);
#if CXImage
            int LoadType = 3;
            switch (_File.Extension.ToLower())
            {
                case ".jpg":
                    LoadType = 3;
                    break;
                case ".tga":
                    LoadType = 7;
                    break;
                case ".png":
                    LoadType = 4;
                    break;
                case ".bmp":
                    LoadType = 1;
                    break;
                case ".jpeg":
                    LoadType = 3;
                    break;
                case ".psd":
                    LoadType = 20;
                    break;
            }

            _PtrXImg = TextureHelper.LoadFile(filename, LoadType);
            _TextureType = (TextureType)TextureHelper.CheckType(_PtrXImg);
            TextureHelper.Destory(_PtrXImg);
#endif
            string json = TextureHelper.Start(_FileFullName);
            JsonData data = JsonMapper.ToObject(json);
            _TextureType = (TextureType)Convert.ToInt32(data["type"].ToString());
        }


    }
}
